package arch.dao.impl.loan;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import arch.dao.loan.LoanFormDao;
import arch.domain.loan.LoanForm;
/**
 * 
 * @author <a mailto="wanglong635@126.com">Long</a>
 *
 */
@Repository("loanFormDao")
public class LoanFormImpl implements LoanFormDao {
	@Resource private JdbcTemplate jdbcTemplate; 
	
	public Integer query(String sql, Object... objects) {
		return this.jdbcTemplate.queryForInt(sql,objects);
	}

	@SuppressWarnings("unchecked")
	public List<LoanForm> queryList(String sql, Object... objects) {
		return this.jdbcTemplate.query(sql,objects,new LoanFormMapper());
	}

	public LoanForm find(Serializable primaryKey) {
		String sql = "select ID, LOANER_ID, LOANER_NAME, LOANER_DEPT_CODE, "
			+" LOANER_DEPT_NAME, TELEPHONE, DAYS, REASON, STATE, AUDITOR_ID, "
			+" AUDITOR_NAME, AUDITOR_DEPT_CODE, AUDITOR_DEPT_NAME, PASS_TIME, "
			+" BACK_TIME, LOCK, EFFECT from LN_FORM where ID = ? ";
		
		try {
			return (LoanForm)this.jdbcTemplate.queryForObject(sql, new Object[]{primaryKey},new LoanFormMapper());
		} catch (EmptyResultDataAccessException e) {
			return null;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void merge(LoanForm entity) {
		String sql = "update LN_FORM set TELEPHONE = ? , DAYS = ? , REASON = ?, STATE = ?,"
			+" AUDITOR_ID = ?,AUDITOR_NAME = ? , AUDITOR_DEPT_CODE = ?, AUDITOR_DEPT_NAME = ?,"
			+" EFFECT = ? where ID = ?  ";
		
		this.jdbcTemplate.update(sql,new Object[]{
				entity.getTelephone(),
				entity.getDays(),
				entity.getReason(),
				entity.getState()==null?null:entity.getState().name(),
				entity.getAuditor()==null?null:entity.getAuditor().getUserId(),
				entity.getAuditor()==null?null:entity.getAuditor().getUserName(),
			    entity.getAuditorDept()==null?null:entity.getAuditorDept().getDeptCode(),
				entity.getAuditorDept()==null?null:entity.getAuditorDept().getDeptName(),
				entity.getEffect(),
				entity.getId()
		});
	}

	public void persist(LoanForm entity) {
		String sql = " insert into LN_FORM (ID, LOANER_ID, LOANER_NAME, LOANER_DEPT_CODE,"
			+" LOANER_DEPT_NAME, TELEPHONE, DAYS, REASON, STATE, AUDITOR_ID, "
			+" AUDITOR_NAME, AUDITOR_DEPT_CODE, AUDITOR_DEPT_NAME, PASS_TIME,"
			+" BACK_TIME, LOCK, EFFECT ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
		
		this.jdbcTemplate.update(sql,new Object[]{
				entity.getId(),
				entity.getLoaner()==null?null:entity.getLoaner().getUserId(),
				entity.getLoaner()==null?null:entity.getLoaner().getUserName(),
				entity.getLoanerDept()==null?null:entity.getLoanerDept().getDeptCode(),
				entity.getLoanerDept()==null?null:entity.getLoanerDept().getDeptName(),
				entity.getTelephone(),
				entity.getDays(),
				entity.getReason(),
				entity.getState()==null?null:entity.getState().name(),
				entity.getAuditor()==null?null:entity.getAuditor().getUserId(),
				entity.getAuditor()==null?null:entity.getAuditor().getUserName(),
				entity.getAuditorDept()==null?null:entity.getAuditorDept().getDeptCode(),
				entity.getAuditorDept()==null?null:entity.getAuditorDept().getDeptName(),
				entity.getPassTime(),
				entity.getBackTime(),
				entity.getLock(),
				entity.getEffect()
		});
	}

	public void remove(Serializable primaryKey) {
		String sql = "delete from LN_FORM where ID = ? ";

		this.jdbcTemplate.update(sql,new Object[]{primaryKey});
	}

	public void update(String sql, Object... objects) {
		this.jdbcTemplate.update(sql,objects);
	}

}
